<?php
namespace app\smallapp\controller;
use app\common\controller\BaseApi;
/**
 * 小程序 boss端
 * */
class Boss extends BaseApi{
	function _initialize() {
		if(!request()->isPost()){
			return $this->api_ajax_error("","非法访问");
		}
		$time=input("time/d","");
		$sign=input("sign/s","");
		if(input("debug")!=1){ 
			if(($time+300)<time()||($time-300)>time()){//接口调用时间操过5分钟
				return $this->api_ajax_error("","链接已经过期");
			}
			if($sign!=md5("ocmcom_bs_".$time)){
				return $this->api_ajax_error("","签名错误！");
			}	
		}
    }
	
	/**
	 * 根据电话号码获取店铺
	 * */
	function GetShopList(){
		$mobile=input("mobile");
		if(strlen($mobile)!=11){
			return $this->api_ajax_error("","手机号码格式错误");
		}
		$parent_ids=db("shop")->where(" id in (select shop_id from m_admin where mobile='$mobile')")->column("parent_id");
		if(empty($parent_ids)){
			return $this->api_ajax_error([],"没有更多的数据");
		}
		$parent_ids=implode(",", $parent_ids);
		$list=db("Shop")->field("id,shop_name")->where("parent_id in ($parent_ids)")->fetchsql(FALSE)->select();
		if(empty($list)){
			return $this->api_ajax_error([],"没有更多的数据");
		}else{
			return $this->api_ajax_success($list);
		}
	}
	
	/**
	 * 获取mc 列表
	 * */
	function GetMcList(){
		$shop_id=input("shop_id");
		$list=db("GymEmployee")->where("shop_id=$shop_id and type=2")->field("id,name")->select();
		if(empty($list)){
			return $this->api_ajax_error([],"没有更多的数据");
		}else{
			return $this->api_ajax_success($list);
		}
	}
	
	/**
	 * 获取mc 列表
	 * */
	function GetPtList(){
		$shop_id=input("shop_id");
		$list=db("GymEmployee")->where("shop_id=$shop_id and type=3")->field("id,name")->select();
		if(empty($list)){
			return $this->api_ajax_error([],"没有更多的数据");
		}else{
			return $this->api_ajax_success($list);
		}
	}
	/**
	 * 登录
	 * http://smartgym.com/smallapp/Boss/Login
	 * */
	function Login(){
		$moblie=input("mobile/s");
		$psw=md5(input("psw/s"));
		
		if(strlen($moblie)!=11){
			return $this->api_ajax_error("","手机号码格式错误！");
		}
		if(strlen($psw)<5){
			return $this->api_ajax_error("","密码长度不足5位！");
		}
		$model=db("Admin")->where(" mobile='$moblie' and password='$psw' ")->find();
		if(empty($model)){
			return $this->api_ajax_error("","账号或密码错误！");
		}else{
			if($model['is_boss']!=1){
				return $this->api_ajax_error("","还没有开通boss 端");
			}
			$data=array(
						"name"=>$model['name'],
						"mobile"=>$model["mobile"],
						"boss_id"=>$model["id"],
			);
			return $this->api_ajax_success($data);
		}
	}
	/*
	 * 获取短信验证码
	 * http://smartgym.com/smallapp/Boss/GetCode
	 * */
	function GetCode(){
		$mobile=input("mobile/s");
		$model=db("Admin")->where("mobile='$mobile'")->find();
		if(empty($model)){
			return $this->api_ajax_error("","手机号码未注册");
		}else{
			if($model['is_boss']!=1){
				return $this->api_ajax_error("","此电话号码未开通boss端");
			}
			if(empty(cache("$mobile"))){
				$result=send_msg_code($mobile);
				if($result['flag']){
					cache("$mobile",$result['data'],60);
					return $this->api_ajax_success($result['data'],"成功");
				}else{
					return $this->api_ajax_error($result['data'],$result['msg']);
				}
			}else{
				return $this->api_ajax_error("","操作频繁，稍后再试！");
			}
		}
	} 
	/**
	 * 修改密码 
	 *  http://smartgym.com/smallapp/Boss/ModifyPsw
	 */
	function ModifyPsw(){
		$mobile=input("mobile/s");
		$new_psw=md5(input("new_psw/s"));
		$old_psw=md5(input("old_psw/s"));
		if(strlen(trim($new_psw))<5){
			return $this->api_ajax_error("","新密码长度必须大于5位");
		}
		$id=db("Admin")->where("mobile='$mobile' and password='$old_psw'")->value("id");
		if(empty($id)){
			return $this->api_ajax_error("","账号或密码错误");
		}else{
			db("Admin")->where("id=$id")->setField(array("password"=>$new_psw));
			return $this->api_ajax_success();
		}
	}
	/**
	 * 找回密码 
	 *  http://smartgym.com/smallapp/Boss/FindPsw
	 */
	function FindPsw(){
		$mobile=input("mobile/s");
		$new_psw=md5(input("new_psw/s"));
		$code=input("code","xxxx");
		if(strlen(trim(input("new_psw/s")))<5){
			return $this->api_ajax_error("","新密码长度必须大于5位");
		}
		if($code!=cache("$mobile")){
			return $this->api_ajax_error("","验证码错误或者过期");
		}
		
		$id=db("Admin")->where("mobile='$mobile'")->value("id");
		if(empty($id)){
			return $this->api_ajax_error("","账号不存在");
		}else{
			db("Admin")->where("id=$id")->setField(array("password"=>$new_psw));
			return $this->api_ajax_success("");
		}
	}
	
	/**
	 * 月度状况
	 * */
	function MonthStatistic(){
		$shop_id=input("shop_id");
		$month=input("month");
		if(empty($shop_id)||empty($month)){
			return $this->api_ajax_error("","参数错误");
		}
		$year_month = (int)str_replace("-", "", $month);
		//场馆流水
		$data['shop_month_captical']=db("GymOrderHistory")->where("shop_id=$shop_id and date_format(create_time,'%Y-%m')='$month' ")->sum("pay_money")+db("GymPtOrderHistory")->where("shop_id=$shop_id and 	DATE_FORMAT(create_time,'%Y-%m')='$month'")->sum("price") + db("GymCabinetOrderHistory")->where("shop_id=$shop_id and 	DATE_FORMAT(create_time,'%Y-%m')='$month'")->sum("money");
//        $data['shop_month_captical'] = db("gym_employee_achievement")->where(array("shop_id" => $shop_id, "year_month" => $year_month))->sum("achievement");
		//新增卡
		$data['shop_card_num']=db("GymOrder")->where("shop_id=$shop_id and date_format(create_time,'%Y-%m')='$month' and is_delete=0")->count(1);
		//新增私教课时
		$data['shop_course_num']=db("GymPtOrder")->where("shop_id=$shop_id and date_format(create_time,'%Y-%m')='$month' ")->sum("course_count");
		//课时消耗
		$data['shop_course_use']=db("GymCourseHistory")->where("shop_id=$shop_id and date_format(create_time,'%Y-%m')='$month' ")->sum("course_count");
		//入场
		$list[]=array("key"=>date("Y-m-d"),"value"=>db("BehaviorLog")->where("shop_id=$shop_id and date_format(create_time,'%Y-%m-%d')='".date('Y-m-d')."'")->count(1));
		$list[]=array("key"=>date("Y-m-d",strtotime('-1 day')),"value"=>db("BehaviorLog")->where("shop_id=$shop_id and date_format(create_time,'%Y-%m-%d')='".date("Y-m-d",strtotime('-1 day'))."'")->count("distinct(card_no)"));
		$list[]=array("key"=>date("Y-m-d",strtotime('-2 day')),"value"=>db("BehaviorLog")->where("shop_id=$shop_id and date_format(create_time,'%Y-%m-%d')='".date("Y-m-d",strtotime('-2 day'))."'")->count("distinct(card_no)"));
		$list[]=array("key"=>date("Y-m-d",strtotime('-3 day')),"value"=>db("BehaviorLog")->where("shop_id=$shop_id and date_format(create_time,'%Y-%m-%d')='".date("Y-m-d",strtotime('-3 day'))."'")->count("distinct(card_no)"));
		$list[]=array("key"=>date("Y-m-d",strtotime('-4 day')),"value"=>db("BehaviorLog")->where("shop_id=$shop_id and date_format(create_time,'%Y-%m-%d')='".date("Y-m-d",strtotime('-4 day'))."'")->count("distinct(card_no)"));
		$list[]=array("key"=>date("Y-m-d",strtotime('-5 day')),"value"=>db("BehaviorLog")->where("shop_id=$shop_id and date_format(create_time,'%Y-%m-%d')='".date("Y-m-d",strtotime('-5 day'))."'")->count("distinct(card_no)"));
		$list[]=array("key"=>date("Y-m-d",strtotime('-6 day')),"value"=>db("BehaviorLog")->where("shop_id=$shop_id and date_format(create_time,'%Y-%m-%d')='".date("Y-m-d",strtotime('-6 day'))."'")->count("distinct(card_no)"));
		$data["list"]=$list;
		return $this->api_ajax_success($data);
	}
	
	/**
	 * 业绩数据--mc
	 * */
	function McStatistic(){
		$shop_id=input("shop_id");
		$month=input("month");
		$mc_id=input("mc_id");
		if(empty($shop_id)||empty($month)||empty($mc_id)){
			return $this->api_ajax_error("","参数错误");
		}
        $year_month = (int)str_replace("-", "", $month);
 		$data["achievement"]= db("gym_employee_achievement")->where(array("shop_id" => $shop_id, "year_month" => $year_month, "employee_id" => $mc_id))->value("achievement");
		$data["buy_count"]=db("GymOrderHistory")->where("member_ship_id=$mc_id and date_format(create_time, '%Y-%m')='$month' and opt_type='购卡'")->count(1);
		$data["buy_again_count"]=db("GymOrderHistory")->where("member_ship_id=$mc_id and  date_format(create_time, '%Y-%m')='$month' and  opt_type='续卡'")->count(1);
		$data["up_count"]=db("GymOrderHistory")->where("member_ship_id=$mc_id and  date_format(create_time, '%Y-%m')='$month' and opt_type='升级'")->count(1);
		$data["new_members"]=db("GymMember")->where("member_ship_id=$mc_id and date_format(create_time, '%Y-%m')='$month' ")->count(1);
		$data["out_menmber"]=db("GymOrderHistory")->where("member_ship_id=$mc_id and  date_format(create_time, '%Y-%m')='$month' and  opt_type='退会'")->count(1);
		$data["subscribe_count"]=db("GymFollowUp")->where("member_ship_id=$mc_id and date_format(create_time, '%Y-%m')='$month' and type=2")->count(1);
		$data["follow_count"]=db("GymFollowUp")->where("member_ship_id=$mc_id and date_format(create_time, '%Y-%m')='$month' and type=1")->count(1);
		return $this->api_ajax_success($data);		
	}
	
	/**
	 * 业绩数据--pt
	 * */
	function PtStatistic(){
		$shop_id=input("shop_id");
		$month=input("month");
		$pt_id=input("pt_id");
		if(empty($shop_id)||empty($month)||empty($pt_id)){
			return $this->api_ajax_error("","参数错误");
		}
        $year_month = (int)str_replace("-", "", $month);
 		$data["achievement"]= db("gym_employee_achievement")->where(array("shop_id" => $shop_id, "year_month" => $year_month, "employee_id" => $pt_id))->value("achievement");
		$data["member_count"]=db("GymPtOrderHistory")->where("pt_id=$pt_id and date_format(create_time, '%Y-%m')='$month' and type=1 and member_id in(select id from m_gym_member where shop_id=$shop_id and date_format(create_time, '%Y-%m')='$month')")->count('DISTINCT(member_id)');
		$data["back_course_count"]=db("GymPtOrderHistory")->where("pt_id=$pt_id and  date_format(create_time, '%Y-%m')='$month' and  type=3")->sum("surplus");
		$data["withdraw_count"]=db("GymPtOrderHistory")->where("pt_id=$pt_id and  date_format(create_time, '%Y-%m')='$month' and  type=2")->sum("surplus");
		$data["new_course_count"]=db("GymPtOrderHistory")->where("pt_id=$pt_id and  date_format(create_time, '%Y-%m')='$month' and  type=1")->sum("surplus");
		$data["consumer_course_count"]=db("GymCourseHistory")->where("pt_id=$pt_id and  date_format(create_time, '%Y-%m')='$month'")->sum("course_count");
		$data["subscribe_count"]=db("GymPtFollow")->where("pt_id=$pt_id and date_format(create_time, '%Y-%m')='$month' and type=2")->count(1);
		$data["follow_count"]=db("GymPtFollow")->where("pt_id=$pt_id and date_format(create_time, '%Y-%m')='$month' and type=1")->count(1);
		return $this->api_ajax_success($data);		
	}
}
